package com.sgb.kjwl.viewmodel.self.packers;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.self.SelfTeamIntroduceEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.packers.SelfPackersManageActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SelfPackersManageModel extends BaseViewModel {

    private SelfPackersManageActivity activity;

    private Map<String, Object> map = new HashMap<>();

    public SelfPackersManageModel(SelfPackersManageActivity activity, SelfRepertory selfRepertory) {
        this.activity = activity;
    }

    public void getTeamIntroduce() {
        map.clear();
        // 首选企业编号，如果企业编号为空，则传用户编号，两者不能同时为空，不能同时都有值
        String companyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
        if ("".equals(companyNo) || "0".equals(companyNo)) {
            map.put("companyNo", "");
            map.put("userNo", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, ""));
        } else {
            map.put("companyNo", companyNo);
            map.put("userNo", "");
        }
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().getTeamIntroduce(map).enqueue(new Callback<BaseEntity<SelfTeamIntroduceEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<SelfTeamIntroduceEntity>> call, Response<BaseEntity<SelfTeamIntroduceEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    activity.updateIntroduceEntity(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<SelfTeamIntroduceEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
